<?php

namespace App\Repository\Impl;

if (!defined('IN_PX'))
    exit;

use App\Repository\Collection;
use PDO;

class CollectionImpl implements Collection {

    //仓储层组件
    private function __Repository($value = 'collection') {}

    private function __Inject($db) {}

    private $_table = '`#@__@collection`';

    public function findAllCount($memberId, $language = 0) {
        return $this->db->table('`#@__@collection` c, `#@__@archives` a')
            ->where('c.`archives_id` = a.`archives_id` AND c.`is_status` = 1 AND c.`member_id` = ? AND c.`language` = ?')
            ->bind(array($memberId, $language))
            ->count();
    }

    public function findAll($memberId, $language = 0, $start = null, $end = null) {
        return $this->db->select('c.`collection_id`, a.*')
            ->table('`#@__@collection` c, `#@__@archives` a')
            ->where('c.`archives_id` = a.`archives_id` AND c.`is_status` = 1 AND c.`member_id` = ? AND c.`language` = ?')
            ->limit($start, $end)
            ->bind(array($memberId, $language))
            ->findAll();
    }

    public function addCollection($post) {
//        $this->db->debug();
        return $this->db->table($this->_table)
            ->row(array(
                'is_type' => '?',
                'archives_id' => '?',
                'member_id' => '?',
                'is_status' => '?',
                'language' => '?',
                'add_date' => '?',
                'release_date' => '?',
            ))
            ->bind($post)
            ->save();
    }

    public function updateCollection($post) {
//        $this->db->debug();
        return $this->db->table($this->_table)
            ->row(array(
                'is_status' => "ABS(is_status - 1)",
                'release_date' => '?',
            ))
            ->where('`is_type` = ? AND `archives_id` = ? AND `member_id` = ?')
            ->bind($post)
            ->update();
    }

    public function deleteCollection($post) {
        $this->db->debug();
        return $this->db->table($this->_table)
            ->row(array(
                'is_status' => 0,
                'release_date' => '?',
            ))
            ->where("`is_type` = ? AND `collection_id` IN ({$post['collection_id']}) AND `member_id` = ?")
            ->bind($post)
            ->update();
    }

    public function chkExists($post) {
        return $this->db->table($this->_table)->where('`is_type` = ? AND `archives_id` = ? AND `member_id` = ?')->bind($post)->exists();
    }
}
